import Request from '@/js_sdk/luch-request/luch-request/index.js'
var baseUrl = 'https://www.jinxicloud.online' /* 根域名 */
//var baseUrl = 'https://soft.scmls.cn' /* 根域名 */
var apiBaseUrl = baseUrl + '/admin/wifi/uapi/'/* 根域名 */
const http = new Request()
var merchantName = 'Admin '
http.baseUrl = baseUrl
http.apiBaseUrl = apiBaseUrl

//请求前拦截，用来动态加参,例如token
http.interceptors.request.use((config) => { // 可使用async await 做异步操作
  config.baseURL = apiBaseUrl
  config.header = {
    ...config.header,
	Authorization: 'Bearer '+ uni.getStorageSync('token'),
	Token: merchantName + uni.getStorageSync('admin_id'), // 演示用
  }
  return config
}, config => { // 可使用async await 做异步操作
  return Promise.reject(config)
})

// 请求后
http.interceptors.response.use((response) => {
  return response
}, (response) => { /*  对响应错误做点什么 （statusCode !== 200）*/
  //未登录时清空缓存跳转
  if(response.statusCode == 401){
	 uni.removeStorageSync('token')
	 console.log(response.config.url)
	 if(response.config.url != 'user'){
		 uni.hideLoading()
		 uni.showModal({
		 	title: '授权登录',
		 	content: '请授权登录',
			showCancel:true,
		 	success: function (res) {
		 		if (res.confirm) {
		 			uni.switchTab({
		 			    url: '/pages/my/my'
		 			});
		 		}
		 	}
		 });
	 }
  }
  return Promise.reject(response)
})

export default http
